package ovm.management.administration.factory;

import ovm.entity.EntityFactoryEntity;
import ovm.entity.EntityFormPropertiesEntity;
import ovm.entity.EntityTablePropertiesEntity;
import ovm.management.AbstractManager;

import javax.persistence.Query;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Jokser
 * Date: 02.06.12
 * Time: 14:22
 * To change this template use File | Settings | File Templates.
 */

public class EntityFactoryManager extends AbstractManager {
    
    public static int addDefaultEntity() {
        try {
            EntityFactoryEntity entity = new EntityFactoryEntity();     
            entity.setEntity("Entity");
            em.getTransaction().begin();
            em.persist(entity);
            em.getTransaction().commit();
        } catch (Exception e) {
            e.printStackTrace();
            em = emf.createEntityManager();
            return UNSUCCESSFUL;
        }
        return SUCCESSFUL;
    }
    
    public static EntityFactoryEntity getFactoryProperties (String entityName) {
        Query query = em.createQuery("SELECT e FROM EntityFactoryEntity e WHERE e.entity = :entityName");
        query.setParameter("entityName", entityName);
        List<EntityFactoryEntity> list = query.getResultList();
        if (list.size() == 0) return null;
        return list.get(0);
    }

    public static List<EntityTablePropertiesEntity> getEntityTableProperties (String entityName) {
        Query query = em.createQuery("SELECT e FROM EntityTablePropertiesEntity e " +
                                     "WHERE e.entity = :entityName ORDER BY e.entity, e.orderNumber");
        query.setParameter("entityName", entityName);
        List<EntityTablePropertiesEntity> list = query.getResultList();
        return list;
    }

    public static List<EntityFormPropertiesEntity> getEntityFormProperties (String entityName) {
        Query query = em.createQuery("SELECT e FROM EntityFormPropertiesEntity e " +
                                     "WHERE e.entity = :entityName ORDER BY e.entity, e.orderNumber");
        query.setParameter("entityName", entityName);
        List<EntityFormPropertiesEntity> list = query.getResultList();
        return list;
    }
    
}
